#include "installappwidget.h"

InstallAppWidget::InstallAppWidget(QWidget *parent)
    : QWidget{parent}
{
    process = new QProcess();

    ddropLabel = new QLabel("拖放区域");
    ddropLabel->setFrameShape(QFrame::Box);
//    ddropLabel->setStyleSheet("border-width: 1px;border-style: solid;border-color: rgb(255, 170, 0);");
    ddropLabel->setAlignment(Qt::AlignCenter);
    selectBtn = new QPushButton("选择安装包");
    installBtn = new QPushButton("开始安装");

    connect(selectBtn, SIGNAL(clicked()), this, SLOT(showFile()));
    connect(installBtn, SIGNAL(clicked()), this, SLOT(installAPK()));

    btnLayout = new QHBoxLayout;
    btnLayout->addWidget(selectBtn);
    btnLayout->addWidget(installBtn);

    mainLayout = new QVBoxLayout(this);
//    layout->addStretch();
    mainLayout->addWidget(ddropLabel);
    mainLayout->addLayout(btnLayout);

    thread = new InstallAPKThread;
    connect(this,SIGNAL(sendFilePathToThread(QString)), thread, SLOT(receiveFilePathFromMain(QString)));
    connect(thread, SIGNAL(finished()), this, SLOT(threadFinsih()));  // 线程完成后触发
    connect(thread, SIGNAL(sendInstallMessageToMain(QString)), this, SLOT(receiveMessageFromThread(QString)));

    dialog = new InstallWaitDialog(this);

    this->setAcceptDrops(true);
}

void InstallAppWidget::showFile()
{
    QString fileName = QFileDialog::getOpenFileName(this, "选择安装文件", "/home", "APK file(*.apk)");
    filePath = fileName;
    ddropLabel->setText(fileName.split("/").last());

}

void InstallAppWidget::dragEnterEvent(QDragEnterEvent *event)
{
    if(event->mimeData()->hasUrls()){
        const QMimeData *mimeData = event->mimeData();
        QList<QUrl> urls = mimeData->urls();
        QString fileName = urls.at(0).toLocalFile();
        // 获取文件类型
        QString fileType = fileName.trimmed().split(".").last();
//        qDebug() << fileType;
        if(fileType == "apk"){
            event->acceptProposedAction(); // 文件符合要求，接受文件
        }

    }else{
        event->ignore();
    }
}

void InstallAppWidget::dropEvent(QDropEvent *event)
{
    const QMimeData *mimeData = event->mimeData();

    if(mimeData->hasUrls())
    {
        QList<QUrl> urls = mimeData->urls();
        QString fileName = urls.at(0).toLocalFile();
        filePath = fileName;
        ddropLabel->setText(fileName.split("/").last());

    }
}

void InstallAppWidget::installAPK()
{
    if(filePath != nullptr){
        QString fileName = filePath.split("/").last();

        QMessageBox::StandardButton reply = QMessageBox::question(this,
                              "安装软件",
                              "确定安装的软件包:"+fileName);
        if(reply == QMessageBox::Yes){
            emit sendFilePathToThread(filePath);// 将安装文件名传递给安装线程
            thread->start(); // 启动线程
            dialog->show();
        }
    }else{
        QMessageBox::critical(this, "安装错误", "请选择安装文件");
    }

}
void InstallAppWidget::receiveMessageFromThread(QString installMessage)
{
    // 接收传递过来的安装信息
    this->installMessage = installMessage;
}


void InstallAppWidget::threadFinsih()
{
    dialog->close();
    if(this->installMessage == "Success"){
        QMessageBox::information(0,
                                 "安装软件",
                                 "恭喜你，安装成功！！！");
        ddropLabel->clear();
        ddropLabel->setText("拖放区域");
    }else{
        QMessageBox::information(0,
                                 "安装中断",
                                 "安装中断，请重试");
        ddropLabel->clear();
        ddropLabel->setText("拖放区域");
    }
}
